package de.dlrg.wms.gui.forms.generic;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import javax.validation.ConstraintViolation;

import org.eclipse.swt.widgets.Composite;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public abstract class GenericForm extends Composite {
	private List<Label> errorLabels = new ArrayList<Label>();
	protected Composite center;
	private Button btnOk, btnCancel;

	private Composite errors;

	protected abstract void okClicked();

	private final void cancelClicked() {
		close();
	}

	protected Observable exitSignaler = new Observable() {
		@Override
		public void notifyObservers() {
			setChanged();
			super.notifyObservers();
		}
	};
	private Label lblTest;

	protected abstract void initData();

	public GenericForm(Composite arg0, int arg1) {
		super(arg0, arg1);
		setLayout(new BorderLayout(0, 0));

		errors = new Composite(this, SWT.NONE);
		errors.setLayoutData(BorderLayout.NORTH);
		errors.setLayout(new GridLayout(1, false));

		Composite south = new Composite(this, SWT.NONE);
		south.setLayoutData(BorderLayout.SOUTH);
		south.setLayout(new FormLayout());

		btnCancel = new Button(south, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				cancelClicked();
			}
		});
		FormData fd_btnCancel = new FormData();
		fd_btnCancel.bottom = new FormAttachment(100, -10);
		fd_btnCancel.right = new FormAttachment(100);
		btnCancel.setLayoutData(fd_btnCancel);
		btnCancel.setText("Cancel");

		btnOk = new Button(south, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				okClicked();
			}
		});
		FormData fd_btnOk = new FormData();
		fd_btnOk.top = new FormAttachment(btnCancel, 0, SWT.TOP);
		fd_btnOk.right = new FormAttachment(btnCancel, -6);
		btnOk.setLayoutData(fd_btnOk);
		btnOk.setText("OK");

		center = new Composite(this, SWT.NONE);
		center.setLayoutData(BorderLayout.CENTER);
	}

	public Observable getClosing() {
		return exitSignaler;
	}

	public void close() {
		setVisible(false);
		exitSignaler.notifyObservers();
	}

	protected void showError(
			javax.validation.ConstraintViolationException constraintViolationException) {
		this.removeLabels();

		for (ConstraintViolation<?> violation : constraintViolationException
				.getConstraintViolations()) {
			System.out.println(violation.getMessage());
			Label errorLabel = new Label(errors, SWT.NONE);
			errorLabel.setText(violation.getMessage());
			this.errorLabels.add(errorLabel);
		}
		this.layout(true);
	}

	private void removeLabels() {
		for (Label label : this.errorLabels) {
			label.dispose();
		}
		this.errorLabels.clear();
		this.layout(true);
	}
}